﻿package controllers.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.User;

import models.Proofrinfo;
import models.Estate2;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class ProofrinfoController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	public View getNewProofrinfoItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		User user = (User) rbac.getCurrentUser();		
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = " Local_ID in (select PF_local_id from ProofrInfo where PF_UnitName = '" +user.getDepartment().getSxname()+ "')";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		
		
		
        
		Object[] args = tmpArgs.toArray();
		
		long total = Estate2.count(Estate2.class, cond, args);
		List<Estate2> items = Estate2.findAll(Estate2.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View viewProofrinfo(int id) throws Exception {
		Estate2 item = Estate2.find(Estate2.class,id);
		User user = (User) rbac.getCurrentUser();
		String content = "找不到指定信息内容。";
		if (item.getUnitname().equals(user.getDepartment().getSxname()))
		{
			Proofrinfo info =  Proofrinfo.findFirst(Proofrinfo.class, "pf_local_id = ? ", new Object[]{ item.getLocal_id()});
    		  if (info != null) {
    			  content = info.getContext();
    		  }
		}
		return new JsonView("success:true,msg:"+content);
	}
	
	public View destroyAllProofrinfo() throws Exception {
		User user = (User) rbac.getCurrentUser();
		List<Proofrinfo> items = Proofrinfo.findAll(Proofrinfo.class, "pf_unitname = ? ", new Object[]{ user.getDepartment().getSxname() });
		
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除校对信息，内容为：" + new JsonView(items));									

		Proofrinfo.execute(Proofrinfo.class,"delete proofrinfo where  pf_unitname = ? ", new Object[]{ user.getDepartment().getSxname() });
		
		return new JsonView("success:true");
		}
		
	
	public View destroyProofrinfo(int id) throws Exception {
		Estate2 item = Estate2.find(Estate2.class,id);
		User user = (User) rbac.getCurrentUser();
		if (item.getUnitname().equals(user.getDepartment().getSxname()))
		{
			List<Proofrinfo> items = Proofrinfo.findAll(Proofrinfo.class, "  pf_local_id = ? ", new Object[]{ item.getLocal_id()});
			
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除校对信息，内容为：" + new JsonView(items));									

			  Proofrinfo.execute(Proofrinfo.class,"delete proofrinfo where  pf_local_id = ? ", new Object[]{ item.getLocal_id()});
    		
		}
		return new JsonView("success:true");
	}
	
}
